package com.guigu.ssyx.sys.service.impl;

import com.atguigu.ssyx.model.sys.RegionWare;
import com.atguigu.ssyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.guigu.ssyx.common.exception.SsyxException;
import com.guigu.ssyx.common.result.ResultCodeEnum;
import com.guigu.ssyx.sys.service.RegionWareService;
import com.guigu.ssyx.sys.mapper.RegionWareMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author 代奉明
* @description 针对表【region_ware(城市仓库关联表)】的数据库操作Service实现
* @createDate 2025-09-04 11:43:50
*/
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare>
    implements RegionWareService{

    // 移除@Autowired，因为ServiceImpl已经提供了mapper实例
    @Autowired
    private RegionWareMapper regionWareMapper;


    @Override
    public IPage<RegionWare> selectPage(Page<RegionWare> regionPage, RegionWareQueryVo regionWareQueryVo) {
        String keyword = regionWareQueryVo.getKeyword();
        LambdaQueryWrapper<RegionWare> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.like(RegionWare::getRegionName, keyword)
                    .or()
                    .like(RegionWare::getWareName, keyword);
        }
        // 使用继承自ServiceImpl的分页方法，而不是直接调用mapper
        IPage<RegionWare> pageModel = this.page(regionPage, queryWrapper);


        return pageModel;

    }

    @Override
    public IPage<RegionWare> selectByPage(Page<RegionWare> regionWarePage, RegionWareQueryVo regionWareQueryVo) {

        String keyword = regionWareQueryVo.getKeyword();
        LambdaQueryWrapper<RegionWare> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.like(RegionWare::getRegionName, keyword)
                    .or()
                    .like(RegionWare::getWareName, keyword);
        }

        IPage<RegionWare> regionWarePage1 = regionWareMapper.selectPage(regionWarePage, queryWrapper);

        return regionWarePage1;

    }

    @Override
    public void saveRegionWare(RegionWare regionWare) {
        LambdaQueryWrapper<RegionWare> regionWareLambdaQueryWrapper = new LambdaQueryWrapper<>();
        regionWareLambdaQueryWrapper.eq(RegionWare::getRegionId,regionWare.getRegionId());
        Integer count = regionWareMapper.selectCount(regionWareLambdaQueryWrapper);
        if(count > 0){
            throw new SsyxException(ResultCodeEnum.REGION_OPEN);
        }
        regionWareMapper.insert(regionWare);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        RegionWare regionWare = regionWareMapper.selectById(id);
        regionWare.setStatus(status);
        regionWareMapper.updateById(regionWare);
    }
}




